package javagprssrv.DataAnalyse;

import javagprssrv.StructClass.structTCPServerSocket;
import java.util.Date;
import javagprssrv.Common.DateTime;
import java.sql.ResultSet;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.Common.CRC;
import javagprssrv.Common.CommonClass;
import javagprssrv.StructClass.structIATask;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structTerm;
import javagprssrv.StructClass.structRecvData;

public class TrdTCPDataTrans extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTerm term;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String AsynTelNo;
    java.lang.String TaskID;
    java.lang.String ZB;
    java.lang.String TelNO;
    java.lang.String YBH;
    java.lang.String WYM;
    java.lang.String TermNO;
    
    public TrdTCPDataTrans(final javagprssrv.StructClass.structRecvData _recvData) {
        super("TrdTCPDataTrans");
        this.term = new javagprssrv.StructClass.structTerm();
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.recvData = _recvData;
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
    }
    
    @java.lang.Override
    public void run() {
        try {
            java.lang.Thread.sleep(500L);
            final int Data_0 = this._Data.charAt(0);
            this.TermNO = this._Data.substring(1, 4);
            final int Data_2 = this._Data.charAt(4);
            this.WYM = this._Data.substring(this._Data.indexOf("YY") + 2, this._Data.indexOf("YY") + 2 + 11);
            if (Data_2 == 15) {
                this.do_xintiao();
            }
            if (this._Data.charAt(0) == 'Y') {
                this.do_59();
            }
            if (this._Data.charAt(0) == 'A') {
                this.locateTask();
                boolean isJSSJ = false;
                try {
                    isJSSJ = this.task.Task_isJSSJ;
                }
                catch (java.lang.Exception ex) {}
                if (isJSSJ) {
                    synchronized (javagprssrv.JGS_Param.IATasks) {
                        for (int idx = javagprssrv.JGS_Param.IATasks.size() - 1; idx >= 0; --idx) {
                            if (((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientIP.equals(this.task.JSSJ_ClientIP) && ((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).ClientPort == this.task.JSSJ_ClientPort) {
                            	((structIATask)javagprssrv.JGS_Param.IATasks.elementAt(idx)).RecvData = this._Data;
                                break;
                            }
                        }
                    }
                }
                else {
                    synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
                        this.recvData.RecvDataType = 2;
                        javagprssrv.JGS_Param.UnSavedRecvData_TCP.add(this.recvData);
                    }
                }
                final int cmd = this._Data.substring(4, 5).charAt(0);
                if (cmd == 243) {
                    return;
                }
                if (cmd == 244) {
                    return;
                }
                if (cmd == 245) {
                    return;
                }
                this.do_41();
            }
        }
        catch (java.lang.Exception ex2) {}
    }
    //结构不符
    private void do_xintiao() {
        java.lang.String res = "";
        res = this._Data.substring(0, 5) + "0000";
        res += new javagprssrv.Common.CommonClass().GetTimeStampString();
        res += "Z";
        res += javagprssrv.Common.CRC.CRC16(res);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, res);
        int idx = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.TCP_Terms.size(); ++i) {
            if (javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(i).Term_RemotePort == this._RemotePort) {
                idx = i;
                break;
            }
        }
        if (idx <= -1) {
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final java.lang.String termno = java.lang.String.valueOf((int)java.lang.Integer.valueOf(this.TermNO)).trim();
            final java.lang.String sql = "select yb.*,yb2.组别 from t_yb yb left outer join t_yb2 yb2 on yb2.仪表号=yb.仪表号 where cast(yb.终端号 as INT)=" + termno + " and 唯一码='" + this.WYM + "'";
            final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            try {
                rs.next();
                this.ZB = rs.getString("组别").trim();
                this.YBH = rs.getString("仪表号").trim();
                this.TelNO = rs.getString("手机卡号").trim();
            }
            catch (java.lang.Exception e) {
                java.lang.String s = "[P=TCP,T=GET ,S=";
                s = s + this._RemoteIP + ":";
                s = s + java.lang.String.valueOf(this._RemotePort) + "] [ 系统未登记该仪表 ] = " + sql;
                javagprssrv.JGS_Param.myLog.ShowLog(s, java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
                return;
            }
            this.term.Term_SerialNo = this.YBH;
            this.term.Term_TelNo = this.TelNO;
            this.term.Term_ZB = this.ZB;
            this.term.Term_TermNo = java.lang.Integer.valueOf(this.TermNO);
            if (this.ZB.length() == 0) {
                this.term.Term_3Xiang = 1;
            }
            else {
                this.term.Term_3Xiang = 3;
            }
            this.term.Term_WYM = this.WYM;
            this.term.Term_RemoteIP = this._RemoteIP;
            this.term.Term_RemotePort = this._RemotePort;
            synchronized (javagprssrv.JGS_Param.TCP_Terms) {
                for (int j = javagprssrv.JGS_Param.TCP_Terms.size() - 1; j >= 0; --j) {
                    if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemoteIP.equals(this._RemoteIP) && javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_RemotePort == this._RemotePort) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                    else if (javagprssrv.JGS_Param.TCP_Terms.elementAt(j).Term_ZB.equals(this.ZB)) {
                        javagprssrv.JGS_Param.TCP_Terms.remove(j);
                    }
                }
                javagprssrv.JGS_Param.TCP_Terms.add(this.term);
            }
        }
    }
    
    private void locateTask() {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    break;
                }
            }
        }
    }
    
    private void do_41() throws java.lang.Exception {
        int idxTask = -1;
        final java.lang.String ip = this.recvData.RemoteIP;
        final java.lang.String port = java.lang.String.valueOf(this.recvData.RemotePort);
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 2) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP.equals(ip) && javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port.equals(port)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    if (this.JobIndex >= javagprssrv.JGS_Param.Tasks.elementAt(i).Task_Jobs.size()) {
                        javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = "2000-01-01 01:02:03";
                        break;
                    }
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_59() throws java.lang.Exception {
        this.AsynTelNo = this._Data.substring(1, 12);
        int idxTask = -1;
        for (int i = 0; i < javagprssrv.JGS_Param.Tasks.size(); ++i) {
            if (javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State == 1) {
                if (javagprssrv.JGS_Param.Tasks.elementAt(i).TaskAsynTelNO.equals(this.AsynTelNo)) {
                    idxTask = i;
                    this.TaskID = javagprssrv.JGS_Param.Tasks.elementAt(i)._TaskID;
                    this.task = javagprssrv.JGS_Param.Tasks.elementAt(i);
                    this.JobIndex = javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex + 1;
                    ++javagprssrv.JGS_Param.Tasks.elementAt(i).Task_JobsIndex;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_StartDateTime = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_IP = this.recvData.RemoteIP;
                    javagprssrv.JGS_Param.Tasks.elementAt(i).BindGPRS_Port = java.lang.String.valueOf(this.recvData.RemotePort);
                    javagprssrv.JGS_Param.Tasks.elementAt(i).Task_State = 2;
                    break;
                }
            }
        }
        if (idxTask == -1) {
            return;
        }
        this.do_nextJob();
    }
    
    private void do_nextJob() throws java.lang.Exception {
        java.lang.String job = this.task.Task_Jobs.elementAt(this.JobIndex).trim().toUpperCase();
        job += "                ";
        if (job.substring(0, 9).equals("GET DATE ")) {
            this.do_getDate(job);
        }
        if (job.substring(0, 9).equals("SET DATE ")) {
            this.do_setDate(job);
        }
        if (job.substring(0, 10).equals("GET MONTH ")) {
            this.do_getMonth(job);
        }
        if (job.substring(0, 10).equals("SET MONTH ")) {
            this.do_setMonth(job);
        }
        if (job.substring(0, 8).equals("GET TIME")) {
            this.do_getTime(job);
        }
        if (job.substring(0, 8).equals("SET TIME")) {
            this.do_setTime(job);
        }
        if (job.substring(0, 9).equals("GET PARAM")) {
            this.do_getParam(job);
        }
        if (job.substring(0, 9).equals("SET PARAM")) {
            this.do_setParam(job);
        }
    }
    
    private void do_getTime(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET TIME ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '\r';
        cmdString += "0000";
        cmdString += "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setTime(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET TIME ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        if (job.substring(9, 21).toUpperCase().equals("$SERVERTIME$")) {
            cmdString = cmdString + '-' + "0000" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2) + "Z";
        }
        else {
            cmdString = cmdString + '-' + "0000" + job.substring(9, 21) + "Z";
        }
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_getParam(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET PARAM ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '\u000e';
        cmdString += "0000";
        cmdString += "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setParam(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET PARAM ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString = cmdString + '.' + "0000" + job.trim().substring(10) + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_getDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("GET DATE ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '\u0005';
        cmdString += job.substring(11, 13);
        cmdString += job.substring(9, 11);
        cmdString += new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4);
        cmdString = cmdString + '0' + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setDate(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 9).equals("SET DATE ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        final int month = java.lang.Integer.valueOf(job.substring(9, 11));
        final int date = java.lang.Integer.valueOf(job.substring(11, 13));
        final java.util.Date dt = new java.util.Date();
        final int currMonth = dt.getMonth() + 1;
        java.lang.String cmd = "";
        if (month == currMonth) {
            cmd = java.lang.String.valueOf('$');
        }
        else {
            cmd = java.lang.String.valueOf('%');
        }
        cmdString += cmd;
        if (date >= 10) {
            cmdString += java.lang.String.valueOf(date);
        }
        else {
            cmdString = cmdString + "0" + java.lang.String.valueOf(date);
        }
        cmdString = cmdString + "00" + new javagprssrv.Common.DateTime().getDateTime("YYYYMMDDHHNNSS").substring(2, 4) + job.substring(9) + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_getMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("GET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '\u0003';
        cmdString += job.substring(10, 12);
        cmdString += new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4);
        cmdString += "00";
        cmdString = cmdString + '0' + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void do_setMonth(java.lang.String job) throws java.lang.Exception {
        job = job.toUpperCase();
        if (!job.substring(0, 10).equals("SET MONTH ")) {
            return;
        }
        job = job.trim();
        java.lang.String cmdString = "A";
        final int zdh = java.lang.Integer.valueOf(this.task.Task_TermNo);
        if (zdh >= 100) {
            cmdString += java.lang.String.valueOf(zdh);
        }
        else if (zdh >= 10) {
            cmdString = cmdString + "0" + java.lang.String.valueOf(zdh);
        }
        else {
            cmdString = cmdString + "00" + java.lang.String.valueOf(zdh);
        }
        cmdString += '#';
        cmdString = cmdString + job.substring(10, 12) + "00";
        cmdString += new javagprssrv.Common.DateTime().getDateTime("YYYYMMDD").substring(2, 4);
        cmdString = cmdString + job.substring(10, 12) + "01";
        cmdString += job.substring(12);
        java.lang.String s = "";
        for (int i = 0; i < 96; ++i) {
            s += "0";
        }
        cmdString = cmdString + s + "Z";
        cmdString += javagprssrv.Common.CRC.CRC16(cmdString);
        this.TCPDataSend(this._RemoteIP, this._RemotePort, cmdString);
    }
    
    private void TCPDataSend(final java.lang.String RemoteIP, final int RemotePort, final java.lang.String strSend) {
        int idx = -1;
        for (int i = javagprssrv.JGS_Param.TCPServerSockets.size() - 1; i >= 0; --i) {
            if (((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemoteIP.equals(RemoteIP) && ((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(i)).RemotePort == RemotePort) {
                idx = i;
                break;
            }
        }
        if (idx > -1) {
        	((structTCPServerSocket)javagprssrv.JGS_Param.TCPServerSockets.elementAt(idx)).SendData(strSend);
        }
    }
}
